/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Project;

import Common.IProject;
import Common.IProjectCollection;
import Common.IProjectModel;
import Common.IUnit;
import Common.IUnitDescriptor;
import Common.IUnitHolder;
import java.util.HashSet;

/**
 *
 * @author Администратор
 */
public class ProjectModel implements IProjectModel, IUnitDescriptor {

    private HashSet<IUnitDescriptor> unitDescriptors;
    private ProjectCollection projects;
    public ProjectModel()
    {
        this.projects = new ProjectCollection();
        this.unitDescriptors = new HashSet<IUnitDescriptor>();
        this.unitDescriptors.add(this);
    }


    // <editor-fold defaultstate="collapsed" desc="IProjectModel Initialize">
    public IProjectCollection getProjects() {
       return this.projects;
    }

    public IProject BuildProject() {
        IProject project = new Project(this);
        projects.add(project);
        return project;
    }
    // </editor-fold>

    public void RegisterUnitDescriptor(IUnitDescriptor descriptor) {
        this.unitDescriptors.add(descriptor);
    }

    public Iterable<IUnitDescriptor> getUnitDescriptors() {
        return this.unitDescriptors;
    }

    public Iterable<IUnitDescriptor> SearchSuitableUnitDescriptors(String parentUnitType) {
        HashSet<IUnitDescriptor> und = new HashSet<IUnitDescriptor>();
        for (IUnitDescriptor uin: unitDescriptors)
        {
            if (uin.SupportsParentType(parentUnitType)){
                    und.add(uin);
            }
        }
        return und;
    }

    public IUnitDescriptor SearchSuitableUnitDescriptor(String unitType, String parentUnitType) {

        IUnitDescriptor ud=null;
        for(IUnitDescriptor uid: unitDescriptors)
        {
            if (uid.CanBuildUnit(unitType, parentUnitType))
            {
                ud=uid;
                break;
            }
        }
        return ud;
    }

    public String getName() {
        return "Project Descriptor";
    }

    public IUnit BuildUnit(IUnitHolder parent) {
        return this.BuildProject();
    }

    public Boolean CanBuildUnit(String unitType, String parentType) {
        return IProject.class.getName().equals(unitType);
    }

    public Boolean SupportsParentType(String parentType) {
        return parentType==null;
    }

}
